/**
 入驻小区
 **/
(function(vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            inspectionTaskInfo: {
                inspectionTasks: [],
                plans:[],
                total: 0,
                records: 1,
                moreCondition: false,
                taskId: '',
                stateTypes: [],
                conditions: {
                    planUserName: '',
                    inspectionPlanId: '',
                    inspectionPlanName: '',
                    actInsTime: '',
                    startTime: '',
                    endTime: '',
                    state: '',
                    orderByDesc: 'desc'
                }
            }
        },
        _initMethod: function() {
            //与字典表关联
            vc.getDict('inspection_task', "state", function(_data) {
                $that.inspectionTaskInfo.stateTypes = _data;
            });
            $that._listInspectionPlans();
            $that._initInspectionTaskDateInfo();
            $that._listInspectionTasks(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function() {
            vc.on('inspectionTask', 'listInspectionTask', function(_param) {
                $that._listInspectionTasks(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            vc.on('inspectionTask', 'pageReload', function(_param) {
                location.reload();
            });
            vc.on('pagination', 'page_event', function(_currentPage) {
                $that._listInspectionTasks(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _initInspectionTaskDateInfo: function() {
                $('.startTime').datetimepicker({
                    language: 'zh-CN',
                    fontAwesome: 'fa',
                    format: 'yyyy-mm-dd hh:ii:ss',
                    initTime: true,
                    initialDate: new Date(),
                    autoClose: 1,
                    todayBtn: true
                });
                $('.startTime').datetimepicker()
                    .on('changeDate', function(ev) {
                        var value = $(".startTime").val();
                        $that.inspectionTaskInfo.conditions.startTime = value;
                    });
                $('.endTime').datetimepicker({
                    language: 'zh-CN',
                    fontAwesome: 'fa',
                    format: 'yyyy-mm-dd hh:ii:ss',
                    initTime: true,
                    initialDate: new Date(),
                    autoClose: 1,
                    todayBtn: true
                });
                $('.endTime').datetimepicker()
                    .on('changeDate', function(ev) {
                        var value = $(".endTime").val();
                        $that.inspectionTaskInfo.conditions.endTime = value;
                    });
                //防止多次点击时间插件失去焦点
                document.getElementsByClassName(' form-control startTime')[0].addEventListener('click', myfunc)

                function myfunc(e) {
                    e.currentTarget.blur();
                }

                document.getElementsByClassName(" form-control endTime")[0].addEventListener('click', myfunc)

                function myfunc(e) {
                    e.currentTarget.blur();
                }
            },
            _listInspectionTasks: function(_page, _rows) {
                $that.inspectionTaskInfo.conditions.page = _page;
                $that.inspectionTaskInfo.conditions.row = _rows;
                $that.inspectionTaskInfo.conditions.communityId = vc.getCurrentCommunity().communityId;
                let param = {
                    params: $that.inspectionTaskInfo.conditions
                };
                param.params.planUserName = param.params.planUserName.trim();
                //发送get请求
                vc.http.apiGet('/inspectionTask.listInspectionTasks',
                    param,
                    function(json, res) {
                        let _json = JSON.parse(json);
                        $that.inspectionTaskInfo.total = _json.total;
                        $that.inspectionTaskInfo.records = _json.records;
                        $that.inspectionTaskInfo.inspectionTasks = _json.inspectionTasks;
                        vc.emit('pagination', 'init', {
                            total: $that.inspectionTaskInfo.records,
                            dataCount: $that.inspectionTaskInfo.total,
                            currentPage: _page
                        });
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _openInspectionTaskDetail: function(_inspectionTask) {
               // vc.emit('inspectionTaskDetail', 'openInspectionTaskDetail', _inspectionTask);
                window.open('/#/pages/inspection/inspectionTaskDetail?taskId='+_inspectionTask.taskId)
            },
            _openInspectionTaskTransfer: function(_inspectionTask) {
                vc.emit('inspectionTaskTransfer', 'openInspectionTaskTransferModal', _inspectionTask);
            },
            _openDeleteInspectionTask: function(_inspectionTask) {
                vc.emit('deleteInspectionTask', 'openDeleteInspectionTaskModal', _inspectionTask);
            },
            //查询
            _queryInspectionTaskMethod: function() {
                $that._listInspectionTasks(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            //重置
            _resetInspectionTaskMethod: function() {
                $that.inspectionTaskInfo.conditions.planUserName = "";
                $that.inspectionTaskInfo.conditions.inspectionPlanId = "";
                $that.inspectionTaskInfo.conditions.inspectionPlanName = "";
                $that.inspectionTaskInfo.conditions.startTime = "";
                $that.inspectionTaskInfo.conditions.endTime = "";
                $that.inspectionTaskInfo.conditions.state = "";
                $that._listInspectionTasks(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _moreCondition: function() {
                if ($that.inspectionTaskInfo.moreCondition) {
                    $that.inspectionTaskInfo.moreCondition = false;
                } else {
                    $that.inspectionTaskInfo.moreCondition = true;
                }
            },
            swatchPlan:function(_plan){
                $that.inspectionTaskInfo.conditions.inspectionPlanId = _plan.inspectionPlanId;
                $that._listInspectionTasks(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _listInspectionPlans: function (_page, _rows) {
                let param = {
                    params: {
                        page:1,
                        row:1000,
                        communityId:vc.getCurrentCommunity().communityId
                    }
                };
                let _plans = [{
                    inspectionPlanName:'全部',
                    inspectionPlanId:''
                }]
                //发送get请求
                vc.http.apiGet('/inspectionPlan.listInspectionPlans',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        _json.data.forEach(_plan => {
                            _plans.push(_plan)
                        });
                        $that.inspectionTaskInfo.plans = _plans;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);